package com.tma.timesheettracker.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import android.content.ContentResolver;
import android.util.Log;

import com.tma.timesheettracker.contentprovider.TimeTrackerContentProvider;
import com.tma.timesheettracker.model.Task;
import com.tma.timesheettracker.model.TrackingEntry;
import com.tma.timesheettracker.model.User;

public class Util {
	public static final String SORTED_TIME_SHEET_TRACKING = "SORTED_TIME_SHEET_TRACKING";
	public static final String TIME_SHEET_TRACKING = "TIME_SHEET_TRACKING";
	public static final String ONE_HOUR_SIZE = "HOUR_SIZE";
	public static final String DAY_TO_RECORD = "DAY_RECORD";
	public static final String ACTION = "ACTION";
	public static final String CREATE = "CREATE";
	public static final String EDIT = "EDIT";
	public static final SimpleDateFormat DATE_FORMATTER = new SimpleDateFormat(
			"yyyy-MM-dd");
	public static final DateFormat dfm = new SimpleDateFormat("yyyy-MM-dd");
	public static final int DEFAULT_USER_ID = 1;
	public static final int MILISECOND = 60 * 60 * 24 * 1000;
	public static final int WORKING_HOURS = 8;
	public static boolean isAdd = false;
	public static int ACTION_CLOSE = 9999;
	public static String WARNING_TASK_LOGGING = "Please set duration value or task name";

	public static List<Task> addTasks() {
		List<Task> lstTask = new ArrayList<Task>();
		lstTask.add((new Task("Development")));
		lstTask.add((new Task("Review")));
		lstTask.add((new Task("Bug Fixing")));
		return lstTask;
	}

	public static List<User> addUser() {
		List<User> lstUser = new ArrayList<User>();
		lstUser.add(new User("Steven", "12345"));
		lstUser.add(new User("David", "12345"));
		lstUser.add(new User("Peter", "12345"));
		return lstUser;
	}

	public static List<TrackingEntry> addTrackingEntry() throws ParseException {
		List<TrackingEntry> lstTracking = new ArrayList<TrackingEntry>();
		lstTracking.add(new TrackingEntry(1, 1, dfm.parse("2012-08-14"), 1));
		lstTracking.add(new TrackingEntry(1, 1, dfm.parse("2012-08-13"), 4));
		lstTracking.add(new TrackingEntry(1, 1, dfm.parse("2012-08-12"), 2));
		lstTracking.add(new TrackingEntry(1, 2, dfm.parse("2012-08-13"), 2));
		lstTracking.add(new TrackingEntry(1, 2, dfm.parse("2012-08-12"), 2));
		return lstTracking;
	}

	public static void addDummyList(ContentResolver resolver)
			throws ParseException {
		if (isAdd == false) {
			List<Task> lstTask = Util.addTasks();
			for (Task task : lstTask) {
				resolver.insert(TimeTrackerContentProvider.TASK_URI,
						TimeTrackerContentProvider.TaskTable
								.toContentValues(task));
			}

			List<User> lstUser = Util.addUser();
			for (User user : lstUser) {
				resolver.insert(TimeTrackerContentProvider.USER_URI,
						TimeTrackerContentProvider.UserTable
								.toContentValues(user));
			}

			List<TrackingEntry> lstTracking = Util.addTrackingEntry();
			for (TrackingEntry entry : lstTracking) {
				resolver.insert(TimeTrackerContentProvider.TrackingEntry_URI,
						TimeTrackerContentProvider.TrackingEntryTable
								.toContentValues(entry));
			}
			isAdd = true;
		}
	}

	public static Date getCurrentDay() {
		return Calendar.getInstance().getTime();
	}

	public static Date getPreviousDay() {
		return new Date(Calendar.getInstance().getTime().getTime() - MILISECOND);
	}

	public static String formatDate(int year, int month, int day) {
		month++;
		String dayPicker = year + "-" + month + "-" + day;
		Date date = null;
		try {
			date = DATE_FORMATTER.parse(dayPicker);
			return DATE_FORMATTER.format(date);
		} catch (ParseException e) {
			Log.e("Date Parser :", "Invalid date passed");
			return null;
		}

	}

	public static String formatDate(Date date) {

		return DATE_FORMATTER.format(date);

	}

	public static String getDateFormat(int year, int month, int day)
			throws ParseException {
		month++;
		String dayPicker = year + "-" + month + "-" + day;
		Date date = DATE_FORMATTER.parse(dayPicker);
		return date.toString();
	}

	public static String getDateFormat(Date date) {
		String[] friendlyDate = date.toString().split(" ");
		return friendlyDate[0] + " " + friendlyDate[1] + " " + friendlyDate[2];
	}

	public static String[] getYearMonthDay(String date) {
		String[] day = date.split("-");
		return day;
	}

}
